package com.gitee.ec.kernel;

import com.sun.jna.Structure;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author hongda.li
 */
public class FileTime extends Structure {

    /**
     * 1601 年 1 月 1 日至 1970 年 1 月 1 日的毫秒差
     */
    private static final long EPOCH_DIFFERENCE = 11644473600000L;

    /**
     * 低位时间
     */
    public int dwLowDateTime;

    /**
     * 高位时间
     */
    public int dwHighDateTime;

    @Override
    protected List<String> getFieldOrder() {
        return Arrays.asList("dwLowDateTime", "dwHighDateTime");
    }

    public Date toDate() {
        return new Date(this.toMillis());
    }

    public long toMillis() {
        long fileTime = ((long) dwHighDateTime << 32) | (dwLowDateTime & 0xFFFFFFFFL);
        return fileTime / 10000 - EPOCH_DIFFERENCE;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        if (!super.equals(o)) {
            return false;
        }
        FileTime fileTime = (FileTime) o;
        return dwLowDateTime == fileTime.dwLowDateTime && dwHighDateTime == fileTime.dwHighDateTime;
    }

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), dwLowDateTime, dwHighDateTime);
    }
}
